package com.rd.student.jike.class2.xuezhixin.Work719;

import java.util.concurrent.Callable;

public class Work03 extends Thread{

    String way;
    C c;

    public Work03(String way,C c) {
        this.way = way;
        this.c = c;
    }

    @Override
    public void run() {
        if(way.equals("add")){
            C.add();
            System.out.println(C.i);
        }else if(way.equals("sub")){
            C.sub();
            System.out.println(C.i);
        }
    }

    public static void main(String[] args) {
        C c = new C();
        Thread t1 = new Work03("add",c);
        Thread t2 = new Work03("sub",c);
        Thread t3 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (c) {
                    try {
                        Thread.sleep(100);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    C.add();
                    System.out.println(C.i);
                }
            }
        });
        Thread t4 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (c) {
                try {
                    Thread.sleep(100);
                }catch (Exception e){
                    e.printStackTrace();
                }
                    C.sub();
                    System.out.println(C.i);
                }
            }
        });

        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }
}
class C{
    static volatile int i = 1;
    public static void add(){
        i++;
    }
    public static void sub(){
        i--;
    }
}

//class Thread3_1 extends Thread{
//    C c;
//
//    public Thread3_1(C c) {
//        this.c = c;
//    }
//
//    @Override
//    public void run() {
//        c.add();
//        System.out.println(c.i);
//        try{
//            Thread.sleep(1000);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//    }
//}
//class Thread3_2 extends Thread{
//    C c;
//
//    public Thread3_2(C c) {
//        this.c = c;
//    }
//
//    @Override
//    public void run() {
//        c.add();
//        System.out.println(c.i);
//        try{
//            Thread.sleep(1000);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//    }
//}
//class Thread3_3 extends Thread{
//    C c;
//
//    public Thread3_3(C c) {
//        this.c = c;
//    }
//
//    @Override
//    public void run() {
//        c.sub();
//        System.out.println(c.i);
//        try{
//            Thread.sleep(1000);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//    }
//}
//class Thread3_4 extends Thread{
//    C c;
//
//    public Thread3_4(C c) {
//        this.c = c;
//    }
//
//    @Override
//    public void run() {
//        c.sub();
//        System.out.println(c.i);
//        try{
//            Thread.sleep(1000);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//    }
//}
